<?php
echo $this->form;
?>
<script type=”text/javascript”>
    $('document').ready(function(){
        ValidateAjax.initialize('formid', '/admin/index/validateform');
    });
ValidateAjax = {
    initialize: function(formid, endpoint){
        end_url = endpoint;
        form_id = '#'+formid;
        $(form_id+' input').blur(function(){
            var formElementID = $(this).attr('id');
            ValidateAjax.doValidate(formElementID);
            return false;
        })
    },
    doValidate: function(id){
        var url = end_url;
        var data = $(form_id).serialize();
        $.post(url,data,function(response){
            $('#'+id).parent().find('.errors').remove();
            if (response[id])
                $('#'+id).parent().append(ValidateAjax.getHTML(response[id]));
        },'json');
    },
    getHTML: function(errArray){
        var o = '<ul class=”errors”>';
        $.each(errArray,function(key,value){
            o+='<li>'+ value+'</li>';
        });
        o+='</ul>';
        return o;
    }
}
</script>
